/*var carselect = new Array();
var storeselect = new Array();
var bmselect = new Array();
var userselect = new Array();
$(function () {
    initThisApp();
})
function initThisApp() {
    // storeselect = [];
    // carselect = [];
    // bmselect = [];
    // userselect = [];
    $.ajax({
        url: "./goPCDD/initDisPage", //请求的url地址
        xhrFields: {
            withCredentials: true
        },
        async:false,
        dataType: "json", //返回格式为json
        type: "GET", //请求方式
        processData: false, //对表单data数据是否进行序列化
        contentType: false, //dataType设置你收到服务器数据的格式
        headers: {
            'Content-Type': 'application/json'
        },
        beforeSend: function() {
            //请求前的处理
        },
        success: function(data) {
            if(data.status==0){
                // top.location.href = "../../login.html";
                window.location.href="./login.html";
            }else{
                var a = JSON.parse(data.data);
                var carnoarr = a.data;
                carselect.splice(0,carselect.length);
                for(var i=0;i<carnoarr.length;i++){
                    var temp = {
                        value: carnoarr[i].CarNo,
                        label: carnoarr[i].CarNo
                    };
                    carselect.push(temp)

                }
                console.log(carselect)
                var storearr = a.data1;
                storearr = storearr.distinct();
                storeselect.splice(0,storeselect.length);
                for(var i=0;i<storearr.length;i++){
                    var temp = {
                        value: storearr[i].Store,
                        label: storearr[i].Store
                    };
                    storeselect.push(temp)
                }
            }
        },
        complete: function() {
            //请求完成的处理
        },
        error: function() {}
    });
}*/
Array.prototype.distinct = function(){
    var arr = this,
        result = [],
        i,
        j,
        len = arr.length;
    for(i = 0; i < len; i++){
        for(j = i + 1; j < len; j++){
            if(arr[i].Store.toString() === arr[j].Store.toString()){
                j = ++i;
            }
        }
        result.push(arr[i]);
    }
    return result;
}

Array.prototype.distinct1 = function(){
    var arr = this,
        result = [],
        i,
        j,
        len = arr.length;
    for(i = 0; i < len; i++){
        for(j = i + 1; j < len; j++){
            if(arr[i].Bm.toString() === arr[j].Bm.toString()){
                j = ++i;
            }
        }
        result.push(arr[i]);
    }
    return result;
}

Array.prototype.distinct2 = function(){
    var arr = this,
        result = [],
        i,
        j,
        len = arr.length;
    for(i = 0; i < len; i++){
        for(j = i + 1; j < len; j++){
            if(arr[i].UserNo.toString() === arr[j].UserNo.toString()){
                j = ++i;
            }
        }
        result.push(arr[i]);
    }
    return result;
}

var pageSize = 99999999;
var vm = new Vue({
    el: "#app",
    data:function(){
        return{
            CarNo:"",
            CarNoList:[],
            //限行信息
            NowDay:"",
            TrafficInfo:"",
            //派车弹框Start
            PaiID:'',
            BmSelect:'',
            UserSelect:'',
            PaiModel:false,
            PaiModelItem:{
                FanPlace:'',
                FanKilometers:'',
                GaosuCost:'',
                StandCost:'',
                OtherCost:'',
                FanCost:'',
                FanRemark:''
            },
            //派车弹框End
            //撤销派车弹框Start
            PaiCheID:'',
            PaiCheMan:'',
            PaiCheCarNo:'',
            PaiCheItem:{
                PaiCheReason:''
            },
            PaiCheModel:false,
            //撤销派车弹框End
            //搜索结果
            columns1: [
                {
                    type: 'index',
                    title: '序号',
                    align: 'center',
                },
                {
                    title: '单据编号',
                    key: 'DocNo',
                    align: 'center',
                },
                {
                    title: '中心店面',
                    key: 'YueStore',
                    align: 'center',
                },
                {
                    title: '司机姓名',
                    key: 'SjName',
                    align: 'center',
                },
                {
                    title: '所派车辆',
                    key: 'PaiCarNo',
                    align: 'center',
                },
                {
                    title: '目的地',
                    key: 'ArrivePlace',
                    align: 'center',
                },
                {
                    title: '派车时间',
                    key: 'PaiDate',
                    align: 'center',
                },
                {
                    title: '预计出发时间',
                    key: 'YueStartDate',
                    align: 'center',
                },
                {
                    title: '预计返回时间',
                    key: 'YueEndDate',
                    align: 'center',
                },
                {
                    title: '派车人',
                    key: 'InputMan1',
                    align: 'center',
                },
                {
                    title: '调度类型',
                    key: 'Status',
                    align: 'center',
                    render:(h,params)=> {
                        var Status = params.row.Status;
                        if(Status=="1"){
                            Status = '已约车';
                        }
                        if(Status=="2"){
                            Status = '已派车';
                        }
                        if(Status=="3"){
                            Status = '已返回';
                        }
                        return h('span',Status);
                    }
                },
                {
                    title: '操作',
                    key: 'action',
                    width:150,
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            /*h('Button', {
                                props: {
                                    type: 'ghost',
                                    size: 'small',
                                    icon:'edit',
                                },
                                attrs: {
                                    title: '派车'
                                },
                                style: {
                                    marginRight: '5px',
                                    border : '0',
                                    fontSize: '16px',
                                },
                                on: {
                                    click: () => {
                                        var Status = params.row.Status;
                                        if(Status=="1"){
                                            this.PaiID=params.row.ID;
                                            this.PaiModel = true;
                                        }
                                        if(Status=="2"){
                                            return null;
                                        }
                                        if(Status=="3"){
                                            return null;
                                        }

                                    }
                                }
                            }),
                            h('span', {

                            },'|'),*/
                            h('Button', {
                                props: {
                                    type: 'ghost',
                                    size: 'small',
                                    icon:'trash-a',
                                },
                                attrs: {
                                    title: '撤销派车'
                                },
                                style: {
                                    marginRight: '5px',
                                    border : '0',
                                    fontSize: '16px',
                                },
                                on: {
                                    click: () => {
                                        var Status = params.row.Status;
                                        if(Status=="1"){
                                            return null;
                                        }
                                        if(Status=="2"){
                                            this.PaiCheMan=params.row.PaiMan;
                                            this.PaiCheCarNo=params.row.PaiCarNo;
                                            this.PaiCheID=params.row.ID;
                                            this.PaiCheModel=true;
                                        }
                                        if(Status=="3"){
                                            return null;
                                        }
                                    }
                                }
                            }),
                            /*h('span', {
                            },'|'),
                            h('Button', {
                                props: {
                                    type: 'ghost',
                                    size: 'small',
                                    icon:'eye',
                                },
                                attrs: {
                                    title: '查看资料'
                                },
                                style: {
                                    marginRight: '5px',
                                    border : '0',
                                    fontSize: '16px',
                                },
                                on: {
                                    click: () => {
                                        this.goLookInfo(params);
                                    }
                                }
                            })*/

                        ]);
                    }


                }
            ],
            columns2: [
                {
                    title: '单据编号',
                    key: 'DocNo',
                    align: 'center',
                },
                {
                    title: '司机姓名',
                    key: 'SjName',
                    align: 'center',
                },
                {
                    title: '所派车辆',
                    key: 'PaiCarNo',
                    align: 'center',
                },
                {
                    title: '派车人',
                    key: 'InputMan1',
                    align: 'center',
                },
                {
                    title: '派车时间',
                    key: 'PaiDate',
                    align: 'center',
                },
            ],
            // 接下来绑定数据，分别对应前面的列的key值来进行数据绑定
            data1: [],
            data2:[],
            pageSize2:5,
            currentPageSize2:1,
            selectYueS:[],
            // 初始化信息总条数
            dataCount:0,
            dataCount2:0,
            stripe:true
        }
    },
    created(){
        this.initPage();
        this.queryAllCarNo();
    },
    methods: {
        initPage(){
            var self = this;
            $.ajax({
                url: "./goPCDD/initPaiPage", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                async:false,
                dataType: "json", //返回格式为json
                type: "GET", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.status==0){
                        window.location.href="./login.html";
                    }else{
                        var trafficArr = JSON.parse(data.data);
                        self.TrafficInfo = trafficArr.data;
                        var d = new Date().getDay();
                        self.NowDay = d.toString();
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });
        },
        queryAllCarNo(){
            var self = this;
            $.ajax({
                url: "./goPCDD/queryAllCarNo", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                type: "GET", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    var carnojson = new Array();
                    var json = JSON.parse(data.data);
                    var tempcarnojson = json.data;
                    var temp = {
                        value: "",
                        label: "全部"
                    };
                    //添加品牌下拉start
                    carnojson.push(temp);
                    for(var i=0;i<tempcarnojson.length;i++){
                        var temp = {
                            value: tempcarnojson[i].CarNo,
                            label: tempcarnojson[i].CarNo
                        };
                        carnojson.push(temp)
                    }
                    self.CarNoList = carnojson;
                },
                error: function() {},
            });
        },
        ok () {
        },
        cancel () {
        },
        handleRowChange(currentRow, oldCurrentRow){
            console.log(currentRow)
            console.log(oldCurrentRow)
        },
        goPaiModel(){
            this.PaiModel = true;
        },
        changeBm(){
            this.BmSelect = null;
            this.PaiModelItem.Bm = '';
            var Store = this.PaiModelItem.Store;
            if(!Store){
                return null;
            }
            var jsondata = {"Store":Store};
            var data_json = JSON.stringify(jsondata)
            var self = this;
            $.ajax({
                url: "./goPCDD/queryBmByStore", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    if(data.status==1){
                        self.BmSelect = '';
                        var a = JSON.parse(data.data);
                        var bmarr = a.data;
                        bmarr = bmarr.distinct1();
                        self.BmSelect = bmarr;
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });

        },
        changeUser(){
            this.UserSelect = '';
            this.PaiModelItem.UserNo = '';
            this.PaiModelItem.Name = '';
            var Store = this.PaiModelItem.Store;
            var Bm = this.PaiModelItem.Bm;
            if(!Store||!Bm){
                return null;
            }
            var jsondata = {"Store":Store,"Bm":Bm};
            var data_json = JSON.stringify(jsondata)
            var self = this;
            $.ajax({
                url: "./goPCDD/queryUserByStoreAndBm", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    if(data.status==1){
                        self.UserSelect = '';
                        var a = JSON.parse(data.data);
                        var userarr = a.data;
                        userarr = userarr.distinct2();
                        self.UserSelect=userarr
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });

        },
        rowClic(a,index){
            var self = this;
            if(self.selectYueS.length>0){
                self.selectYueS = [];
            }
            self.selectYueS .push(a)
        },
        //8 移除选中人员
        removeYue(index){
            var yue = this.selectYueS[index];
            this.selectYueS.splice(index,1);
        },
        getPai(){
            var info ;
            var total;
            var jsondata={"page":this.currentPageSize2, "rows":5};
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goPCDD/queryAllPaiDis", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    info = eval(data.data);
                    total = data.totalnum;
                },
                error: function() {},
            });
            if(info){
                this.data2 = info;
                this.dataCount2 = total;
            }
        },
        querytable(pageNum,pageSize){
            var info ;
            var total;
            var CarNo = this.CarNo;
            var jsondata={"page":pageNum, "row":pageSize,"CarNo":CarNo};
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goPCDD/queryAllPaiDis", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    info = eval(data.data);
                    total = data.totalnum;
                },
                error: function() {},
            });
            if(info){
                this.data1 = info;
                this.dataCount = total;
            }
        },
        // 2 分页
        changepage(index){
            this.querytable(index,pageSize);
        },
        changepage2(index){
            this.currentPageSize2 = index;
            this.getYue();
        },
        //判断是否为面积
        isMianJi(mj){
            var reg = /^[1-9]+\d*(\.\d{0,2})?$|^0?\.\d{0,2}$/;
            if(reg.test(mj)){
                return true;
            }else{
                return false;
            }
        },

        //3 修改
        edit(params){
            var self = this
            self.editDataModalFlag = true
            self.categoreObject = params.row;
        },
        //撤销派车为已约车状态
        delPaiInfo(params){
            var self = this;
            var EditNameStr = parent.$("#uname1").text();
            var PaiCheMan = EditNameStr;
            var ID = this.PaiCheID;
            var PaiCarNo = this.PaiCheCarNo;
            var PaiMan = this.PaiCheMan;
            var PaiCheReason = this.PaiCheItem.PaiCheReason;
            if(PaiCheReason.trim()==''||PaiCheReason==null){
                toastr.warning("撤销失败，请填写撤销原因");
                return false;
            }
            var jsondata = {"ID":ID,"PaiCheReason":PaiCheReason,"PaiCheMan":PaiCheMan,"PaiMan":PaiMan,"PaiCarNo":PaiCarNo};
            var data_json = JSON.stringify(jsondata)
            $.ajax({
                url: "./goPCDD/delPaiInfo", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    if(data.status=="1"){
                        self.PaiCheItem.PaiCheReason = '';
                        toastr.success('撤销派车成功');
                        self.PaiModelItem.UserNo = '';
                        self.PaiModelItem.Name = '';
                        self.PaiModelItem.CarNo = '';
                        self.PaiModelItem.Store = '';
                        self.PaiModelItem.Bm = '';
                        self.PaiModelItem.PaiIsOrNotPin = '';
                    }else{
                        toastr.error('撤销失败，请重新撤销');
                    }
                },
                error: function() {
                    toastr.error('删除失败，请刷新页面');
                },
            });
            this.querytable("1","99999999999");
        },

        SaveFanInfo(params){
            var selectYueS = this.selectYueS;
            var self = this;
            var EditNameStr = parent.$("#uname1").text();
            var InputMan1 = EditNameStr;
            var FanPlace = this.PaiModelItem.FanPlace;
            if(selectYueS.length<=0){
                toastr.warning("请选择约车单据")
                return null;
            }
            if(FanPlace==''||FanPlace==null){
                toastr.warning("请填写车辆停放位置")
                return null;
            }
            var FanKilometers = this.PaiModelItem.FanKilometers;
            if(FanKilometers ==''||FanKilometers ==null){
                toastr.warning("请填写里程数")
                return null;
            }
            if(!this.isMianJi(FanKilometers)){
                toastr.warning("里程数请填写小数点最多为两位的正数");
                return null;
            }

            var GaosuCost = this.PaiModelItem.GaosuCost;
            if(GaosuCost ==''||GaosuCost ==null){
                toastr.warning("请填写高速费")
                return null;
            }
            if(!this.isMianJi(GaosuCost)){
                toastr.warning("高速费请填写小数点最多为两位的正数");
                return null;
            }

            var StandCost = this.PaiModelItem.StandCost;
            if(StandCost ==''||StandCost ==null){
                toastr.warning("请填写停车费")
                return null;
            }
            if(!this.isMianJi(StandCost)){
                toastr.warning("停车费请填写小数点最多为两位的正数");
                return null;
            }

            var OtherCost = this.PaiModelItem.OtherCost;
            if(OtherCost ==''||OtherCost ==null){
                toastr.warning("请填写其他费用")
                return null;
            }
            if(!this.isMianJi(OtherCost)){
                toastr.warning("其他费用请填写小数点最多为两位的正数");
                return null;
            }

            var FanCost = this.PaiModelItem.FanCost;
            if(FanCost ==''||FanCost ==null){
                toastr.warning("请填写总费用")
                return null;
            }
            if(!this.isMianJi(FanCost)){
                toastr.warning("总费用请填写小数点最多为两位的正数");
                return null;
            }

            var FanRemark = this.PaiModelItem.FanRemark;
            var jsondata = {"selectYueS":selectYueS,"FanPlace":FanPlace,"FanKilometers":FanKilometers,"GaosuCost":GaosuCost,"StandCost":StandCost,"OtherCost":OtherCost,"FanCost":FanCost,"FanRemark":FanRemark};
            var data_json = JSON.stringify(jsondata)
            $.ajax({
                url: "./goPCDD/SaveFanInfo", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    if(data.status=="1"){
                        toastr.success('派车返回成功');
                        self.selectYueS = [];
                        self.data2 = [];
                        self.dataCount2 = 0;
                        self.PaiModelItem.FanPlace='';
                        self.PaiModelItem.FanKilometers='';
                        self.PaiModelItem.GaosuCost='';
                        self.PaiModelItem.StandCost='';
                        self.PaiModelItem.OtherCost='';
                        self.PaiModelItem.FanCost='';
                        self.PaiModelItem.FanRemark='';
                    }else{
                        toastr.error('派车返回失败，请重新派车');
                    }
                },
                error: function() {
                    toastr.error('派车返回失败，请刷新页面');
                },
            });
            this.querytable("1","99999999999");
        },
        /*上传查看资料 开始*/
        // 上传授权书前的事件钩子
        handleUpload (file) {
            var self = this;
            var ID = this.SHID;
            // 选择文件后 这里判断文件类型 保存文件 自定义一个keyid 值 方便后面删除操作
            if(this.file.length>=1){
                this.$Notice.warning({
                    title: '警告',
                    desc: '只允许上传一张图片'
                });
                return false;
            }
            // 保存文件到总展示文件数据里
            this.file.push(file);
            // 保存文件到需要上传的文件数组里
            this.uploadFile.push(file)
            //预览
            var reader = new FileReader();
            reader.readAsDataURL(file);
            this.format=['jpg','jpeg','png'];
            // reader.onload = function () {
            //     self.src = reader.result;
            // }
            setTimeout(() => {
                if (!self.src){
                    self.src=reader.result;
                }
            }, 40);
            // 返回 falsa 停止自动上传 我们需要手动上传
            return false;
        },
        //上传执照
        handleUpload1 (file1) {
            var self = this;
            var ID = this.SHID;
            // 选择文件后 这里判断文件类型 保存文件 自定义一个keyid 值 方便后面删除操作
            if(this.file1.length>=1){
                this.$Notice.warning({
                    title: '警告',
                    desc: '只允许上传一张图片'
                });
                return false;
            }
            // 保存文件到总展示文件数据里
            this.file1.push(file1);
            // 保存文件到需要上传的文件数组里
            this.uploadFile1.push(file1)
            //预览
            var reader = new FileReader();
            reader.readAsDataURL(file1);
            this.format=['jpg','jpeg','png'];
            // reader.onload = function () {
            //     self.src1 = reader.result;
            // }
            setTimeout(() => {
                if (!self.src1){
                    self.src1=reader.result;
                }
            }, 100);
            // 返回 falsa 停止自动上传 我们需要手动上传
            return false;
        },
        //上传身份证
        handleUpload2 (file2) {
            var self = this;
            var ID = this.SHID;
            // 选择文件后 这里判断文件类型 保存文件 自定义一个keyid 值 方便后面删除操作
            if(this.file2.length>=1){
                this.$Notice.warning({
                    title: '警告',
                    desc: '只允许上传一张图片'
                });
                return false;
            }
            // 保存文件到总展示文件数据里
            this.file2.push(file2);
            // 保存文件到需要上传的文件数组里
            this.uploadFile2.push(file2)
            //预览
            var reader = new FileReader();
            reader.readAsDataURL(file2);
            this.format=['jpg','jpeg','png'];
            // reader.onload = function () {
            //     self.src2 = reader.result;
            // }
            setTimeout(() => {
                if (!self.src2){
                    self.src2=reader.result;
                }
            }, 40);
            // 返回 falsa 停止自动上传 我们需要手动上传
            return false;
        },

        delectFile (keyID) { // 删除文件
            // 删除总展示文件里的当前文件
            this.file.splice(0);
            // 删除需要上传文件数据里的当前文件
            this.uploadFile.splice(0);
            this.src=null;
        },
        delectFile1 (keyID) { // 删除文件
            // 删除总展示文件里的当前文件
            this.file1.splice(0);
            // 删除需要上传文件数据里的当前文件
            this.uploadFile1.splice(0);
            this.src1=null;
        },
        delectFile2 (keyID) { // 删除文件
            // 删除总展示文件里的当前文件
            this.file2.splice(0);
            // 删除需要上传文件数据里的当前文件
            this.uploadFile2.splice(0);
            this.src2=null;
        },

        upload () { // 上传文件
            for (let i = 0; i < this.uploadFile.length; i++) {
                let item = this.file[i];
                this.$refs.upload.post(item);
            }
        },
        upload1 () { // 上传文件
            for (let i = 0; i < this.uploadFile1.length; i++) {
                let item = this.file1[i];
                this.$refs.upload1.post(item);
            }
        },
        upload2 () { // 上传文件
            for (let i = 0; i < this.uploadFile2.length; i++) {
                let item = this.file2[i]
                this.$refs.upload2.post(item);
            }
        },
        uploadSuccess(){
            toastr.success("上传授权书成功")
        },
        uploadSuccess1(){
            toastr.success("上传执照成功")
        },
        uploadSuccess2(){
            toastr.success("上传身份证成功")
        },
        /*上传查看资料结束*/
        /*查看资料*/
        goLookInfo(params){
            var ID = params.row.ID;
            window.location.href="./car_look.html?CarID="+ID;
        }
        /*查看资料结束*/

    }
})